using UnityEngine;

[ExecuteInEditMode]
public class ControllerGeneric : MonoBehaviour
{
	public static float floorHeight = 0.5f;

	public RemoteControl remoteControl;

	public static int remotesCount;

	public bool needsRefresh;

	private GameObject levelParent;

	private void Awake()
	{
		if (Application.isPlaying)
		{
			LevelController.LevelInPlaceEvent += OnLevelInPlace;
			LevelController.LevelReadyEvent += OnLevelReady;
		}
	}

	private void OnDestroy()
	{
		if (Application.isPlaying)
		{
			LevelController.LevelInPlaceEvent -= OnLevelInPlace;
			LevelController.LevelReadyEvent -= OnLevelReady;
		}
	}

	public virtual void OnLevelInPlace(Transform levelTransform)
	{
	}

	public virtual void OnLevelReady(Transform levelTransform)
	{
	}

	public void InitRemoteControl()
	{
		if (remoteControl == null)
		{
			GameObject gameObject = GameObject.Find("Remotes");
			if (gameObject == null)
			{
				gameObject = new GameObject("Remotes");
			}
			if (gameObject.GetComponent<DestroyIfNoChildren>() == null)
			{
				gameObject.AddComponent<DestroyIfNoChildren>();
			}
			GameObject gameObject2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
			gameObject2.transform.parent = gameObject.transform;
			gameObject2.name = "rc" + remotesCount.ToString("0000") + " ";
			remotesCount++;
			gameObject2.GetComponent<Renderer>().materials = new Material[0];
			BoxCollider component = gameObject2.GetComponent<BoxCollider>();
			component.isTrigger = true;
			remoteControl = gameObject2.AddComponent<RemoteControl>();
			remoteControl.controller = base.gameObject;
			remoteControl.remotesParent = gameObject.transform;
		}
	}

	public bool IsRemoteControlActive()
	{
		if (remoteControl != null)
		{
			return remoteControl.isActive;
		}
		return false;
	}

	public void SelectRemoteControl(GameObject sender)
	{
	}

	public virtual void CopyFromRemoteControl(RemoteControl rc)
	{
		if (remoteControl == null)
		{
			base.gameObject.SendMessage("SetupRemoteControl");
		}
		remoteControl.gameObject.transform.parent = rc.gameObject.transform.parent;
		remoteControl.SetPosition(rc.gameObject.transform.position);
		remoteControl.gameObject.transform.rotation = rc.gameObject.transform.rotation;
		remoteControl.gameObject.transform.localScale = rc.gameObject.transform.localScale;
	}

	public void CopyParentingFromRemoteControl(RemoteControl rc)
	{
		if (remoteControl == null)
		{
			base.gameObject.SendMessage("SetupRemoteControl");
		}
		remoteControl.SetPlacementParent(rc.placementParent);
		remoteControl.alignX = rc.alignX;
		remoteControl.alignY = rc.alignY;
		remoteControl.alignZ = rc.alignZ;
		remoteControl.alignXOffset = rc.alignXOffset;
		remoteControl.alignYOffset = rc.alignYOffset;
		remoteControl.alignZOffset = rc.alignZOffset;
	}

	public void SetRemoteControl(RemoteControl rc)
	{
		remoteControl = rc;
	}

	private GameObject GetLevelParent()
	{
		if (levelParent == null)
		{
			levelParent = GameObject.Find("LEVEL");
			if (levelParent == null)
			{
				levelParent = new GameObject("LEVEL");
				levelParent.AddComponent<LevelController>();
				levelParent.AddComponent<DestroyIfNoChildren>();
			}
		}
		return levelParent;
	}

	public void AssureParenting()
	{
		GameObject gameObject = GetLevelParent();
		if (base.transform.parent != gameObject.transform)
		{
			base.transform.parent = GetLevelParent().transform;
		}
	}
}
